package com.haitaoit.chengquan.module.center.activity.return_goods;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.center.model.MyReturnDetailModel;
import com.haitaoit.chengquan.module.home.adapter.forum.ForumCenterImgAdapter;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.MyGridView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyReturnDetailActivity extends BaseActivity {
    @ViewInject(R.id.return_time)
    TextView return_time;
    @ViewInject(R.id.return_imgll)
    LinearLayout return_imgll;
    @ViewInject(R.id.refund_money)
    TextView refund_money;
    @ViewInject(R.id.refund_reason)
    TextView refund_reason;
    @ViewInject(R.id.instructions)
    TextView instructions;
    @ViewInject(R.id.return_img)
    ImageView return_img;
    @ViewInject(R.id.return_status)
    TextView return_status;
    @ViewInject(R.id.type)
    TextView type;
    @ViewInject(R.id.refund_money2)
    TextView refund_money2;
    @ViewInject(R.id.addtime)
    TextView addtime;
    @ViewInject(R.id.img_gridview)
    MyGridView img_gridview;


    private String order_no;
    private MyReturnDetailModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_my_return_detail);
        setTitle_V("订单详情");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);


        order_no = getIntent().getStringExtra("order_no");
//        return_imgll.setVisibility(View.VISIBLE);  // 退款成功时隐藏
        httpGetorder_after_sales_model();
    }

    private void httpGetorder_after_sales_model() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);
        showProgrees("");
        HttpUtilsSingle.doGet(this, false, Constant.Getorder_after_sales_model, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    Gson gson = new Gson();
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            hideProgree();
                            return;
                        case 0:// 成功
                            model = gson.fromJson(Response, MyReturnDetailModel.class);
                            // status:状态1退款中2退款成功3退款失败
                            if (model.getStatus().equals("1")) {
                                return_img.setImageResource(R.mipmap.pic49);
                                return_status.setText("退款中");
                                refund_money.setText("退款金额：" + model.getRefund_money() + "元");
                                return_time.setVisibility(View.GONE);// 退款成功时显示
                            } else if (model.getStatus().equals("2")) {
                                return_img.setImageResource(R.mipmap.pic32);
                                refund_money.setText("退款金额：" + model.getRefund_money() + "元"+"(已退回余额)");
                                return_status.setText("退款成功");
                            } else if (model.getStatus().equals("3")) {
                                return_img.setImageResource(R.mipmap.pic49_1);
                                return_status.setText("退款失败");
                                refund_money.setText("退款金额：" + model.getRefund_money() + "元");
                                return_time.setVisibility(View.GONE);// 退款成功时显示
                            }
                            refund_money2.setText(model.getRefund_money() + "元");
                            return_time.setText("退款时间：" + model.getRefund_time());
                            type.setText(model.getType());
                            refund_reason.setText(model.getRefund_reason());
                            instructions.setText(model.getInstructions());
                            addtime.setText(model.getAddtime());

                            if (model.getList_img() == null || model.getList_img().size() == 0) {
                                return_imgll.setVisibility(View.GONE);
                            } else {
                                return_imgll.setVisibility(View.VISIBLE);
                                ForumCenterImgAdapter adapter = new ForumCenterImgAdapter(MyReturnDetailActivity.this);
                                adapter.setList(model.getList_img());
                                img_gridview.setAdapter(adapter);
                            }
                            hideProgree();
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    hideProgree();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
                hideProgree();
            }
        });
    }
}
